package pl.edu.agh.transstat.parsing;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileParser {

	private String dir;
	public FileParser(String dir) {
		this.dir = dir;
	}

	public Map<String, List<Double>> getResults() {
		BufferedReader br = null;
		try {
			Map<String, List<Double>> results = new HashMap<String, List<Double>>();
			br = new BufferedReader(new FileReader(dir));
			String line = br.readLine();
			String[] params = line.split("\t");
			int agentNr = 0;
			while((line = br.readLine())!=null && !line.equals("")){
				String[] lineRes = line.split("\t");
				if(agentNr == 0){
					for (int i=1;i<params.length;i++)
						results.put(params[i],new ArrayList<Double>());
				}
				for(int i=1;i<lineRes.length;i++){
					results.get(params[i]).add(Double.parseDouble(lineRes[i]));
				}
				agentNr++;
			}
			List<Double> value = new ArrayList<Double>();
			value.add(Double.valueOf(agentNr));
			results.put("Number of holons",value);
			
			//summary values
			while(!(line = br.readLine().trim()).equals("SUMMARY"));
			line = br.readLine();
			params = line.split("\t");
			line = br.readLine();
			String[] lineRes = line.split("\t");
			for (int i=0;i<params.length;i++){
				value = new ArrayList<Double>();
				value.add(Double.parseDouble(lineRes[i]));
				results.put(params[i], value);
			}
			return results;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if (br!=null)
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return null;
	}

}
